**Housekeeping
clear all
cap log close
log using ${rep_root}/logs/inv_sup_status_montonicity.log, text replace
set more off

local seed = 9999
 
local case_lb = 50

local covars vic_female_any vic_white_any acc_white acc_male acc_old cat_3 cat_4 d_mo_* d_dist_* arrests_pre complaints_pre force_pre any_prior_complaint tenure

*****************
**BRING IN CASE STATUS + COMPLAINTS DATA AND APPLY DATA RESTRICTION
***************** 

do ${rep_root}/code/data_restrict.do	
 
 
*****************
**MERGE WITH OUTCOMES DATA
*****************  
merge 1:1 cr_id acc_id using ${rep_root}/data/outcomes, keep(3) nogen 

**drop if accused are not police officers
//drop if acc_rank != "POLICE OFFICER"

**Drop investigators without enough cases
tab inv_id
sort inv_id 
by inv_id: gen inv_ct = _N
drop if inv_ct<`case_lb'
tab inv_id intake_yr

**Define some covariates
gen po = acc_rank == "POLICE OFFICER"
gen minor_inj = vic_inj_1<=1
gen vic_old = (year(inc_complaint_dt_1) - vic_byr_oldest)>=35 & !missing(vic_byr_oldest)
gen acc_old = acc_age>=35 & !missing(acc_age)
gen cat_3 = acc_cat==3
gen cat_4 = acc_cat==4
 
 

forvalues i = 1/25{
	gen d_dist_`i' = inc_district_1==`i'
}
forvalues yr = 2006/2008{
	gen d_yr_`yr' = intake_yr==`yr'

	forvalues m = 1/12{
		gen d_mo_yr_`m'_`yr' = intake_yr==`yr' & intake_mo==`m'
	}
	
}
forvalues yr = 2014/2018{
	gen d_yr_`yr' = intake_yr==`yr'
	
	forvalues m = 1/12{
		gen d_mo_yr_`m'_`yr' = intake_yr==`yr' & intake_mo==`m'
	}
}

 

**Define treatment

 

gen treat_affidavit = finding_cd != "NO AFFIDAVIT" 
gen treat_penalty = finding_cd == "SUSTAINED" | finding_cd == "NOT SUSTAINED"   
gen treat_sustain = finding_cd == "SUSTAINED"   

/*
Monotonicity Samples:
1) All
2) Cat 4 only
3) Not cat 4
4) Accused officer white
5) Accused officer nonwhite

*/
gen mono_samp_1 = 1
gen mono_samp_2 = cat_4
gen mono_samp_3 = !cat_4
gen mono_samp_4 = acc_white
gen mono_samp_5 = !acc_white
gen mono_samp_6 = !vic_white_any
gen mono_samp_7 = any_prior_complaint==1
gen mono_samp_8 = tenure<10
gen mono_samp_9 = tenure>=10

foreach t in treat_affidavit treat_penalty treat_sustain{
	 qui forvalues s= 1/9{
		preserve
		keep if mono_samp_`s'==1
		**Create LOM
		capture drop resid cr_resid cr_ct inv_tot_resid case inv_ct2  
		qui reg `t' `covars'
		predict resid, residuals

		sort cr_id
		by cr_id: egen cr_resid = total(resid)
		by cr_id: gen cr_ct = _N

		sort inv_id intake_yr intake_mo
		gen case = 1
		by inv_id intake_yr: egen inv_tot_resid = total(resid)
		by inv_id intake_yr: egen inv_ct2 = total(case)
		gen lom_`s' = (inv_tot_resid - cr_resid)/(inv_ct2-cr_ct)

		**First stage
		reg `t' lom_`s' `covars', cluster(inv_id)
		local N_`s' = e(N)
		local first = _b[lom]
		noisily display(`first')
		noisily boottest lom_`s', weight(webb) nograph seed(`seed')
		local first_F = r(F)
		local first_p = r(p)

		local first_`s'_`t' : di %6.3f `first'
		local first_p : di %6.3f `first_p'
		local first_F_`s'_`t': di %6.3f `first_F'

		sort inv_id
		by inv_id: egen inv_lom_`s' = mean(lom_`s')
		by inv_id: egen inv_treat_`s' = mean(`t')
		by inv_id: keep if _n==1

		keep inv_id inv_lom_`s' inv_treat_`s'

		tempfile mono_`s'
		save `mono_`s''
		restore
	}
	
	preserve
	sort inv_id
	by inv_id: keep if _n==1

	forvalues s=1/9{
		merge 1:1 inv_id using `mono_`s'', nogen
	}
	
	local min_corr = 1
	forvalues s = 1/9{
		forvalues u = 1/9{
			qui corr inv_lom_`s' inv_lom_`u'
			if(r(rho)<`min_corr') {
				local min_corr = r(rho)
			}
		}
	}
	local min_corr_`t' : di %6.3f `min_corr'
	display("`min_corr_`t''")
	corr inv_lom_*

	restore
}

texdoc init ${rep_root}/output/tables/monotonicity.tex, replace force
tex \begin{tabular}{l*{2}{c}}
tex \hline\hline
tex                    &\multicolumn{1}{c}{Affidavit}&\multicolumn{1}{c}{N}\\
tex \hline 
tex Full Sample & `first_1_treat_affidavit' &  `N_1' \\
tex  & [`first_F_1_treat_affidavit'] &  \\
tex Force Complaints & `first_2_treat_affidavit' & `N_2' \\
tex  & [`first_F_2_treat_affidavit'] &  \\
tex Nonforce Complaints & `first_3_treat_affidavit' & `N_3'  \\
tex  & [`first_F_3_treat_affidavit'] &  \\
tex White Officer & `first_4_treat_affidavit' & `N_4'  \\
tex  & [`first_F_4_treat_affidavit'] &  \\
tex Nonwhite Officer & `first_5_treat_affidavit' & `N_5'  \\
tex  & [`first_F_5_treat_affidavit'] &   \\
tex All Victims Black & `first_6_treat_affidavit' & `N_6'  \\
tex  & [`first_F_6_treat_affidavit'] &  \\
tex Any Prior Complaints & `first_7_treat_affidavit' & `N_7'  \\
tex  & [`first_F_7_treat_affidavit'] &  \\
tex Low Tenure Officer & `first_8_treat_affidavit' & `N_8'  \\
tex  & [`first_F_8_treat_affidavit'] &  \\
tex High Tenure Officer & `first_9_treat_affidavit' & `N_9'  \\
tex  & [`first_F_9_treat_affidavit'] &  \\
tex \hline
tex Minimum Correlation & `min_corr_treat_affidavit' & \\
tex \hline\hline
tex \end{tabular}

texdoc close

clear
log close

